package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.warning.domain.entity.Region;
import com.ruoyi.warning.domain.query.RegionQuery;
import com.ruoyi.warning.mapper.RegionMapper;
import com.ruoyi.warning.service.IRegionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-04-01
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    @Override
    public Object getAllRegion(RegionQuery query) {
        Page<Region> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        LambdaQueryWrapper<Region> wrapper = new QueryWrapper<Region>().lambda()
                .like(ObjectUtils.isNotEmpty(query.getName()),Region::getName,query.getName())
                .eq(ObjectUtils.isNotEmpty(query.getId()),Region::getId,query.getId())
                .eq(ObjectUtils.isNotEmpty(query.getLevel()),Region::getLevel,query.getLevel())
                .eq(ObjectUtils.isNotEmpty(query.getParentId()),Region::getParentId,query.getParentId())
                .orderByAsc(Region::getSort);
        List<Region> list = this.list(wrapper);
        TableDataInfo tableDataInfo = new TableDataInfo(list, page.getTotal());
        return tableDataInfo;
    }

    @Override
    public Object addOrUpdateRegion(Region region) {
        this.saveOrUpdate(region);
        return null;
    }

    @Override
    public Object deleteRegion(String id) {
        this.removeById(id);
        return null;
    }
}
